package com.jshoperxms.controller.mall.backstage.goodsspecial;

import com.jshoperxms.Response;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.mall.backstage.vo.products.ProductSearchResultVo;
import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.json.GsonJson;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.jshoperxms.controller.utils.statickey.WarnWrods;
import com.jshoperxms.entity.GoodsSpecialContentT;
import com.jshoperxms.entity.GoodsSpecialT;
import com.jshoperxms.service.GoodsSpecialContentTService;
import com.jshoperxms.service.GoodsSpecialTService;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 16/10/19.
 * Des:专题商品内容
 */
@Controller
@RequestMapping("/mall/goodsspecialcontent")
public class GoodsSpecialContentTController extends BaseTController implements IBaseCtrl<GoodsSpecialContentT> {

    @Resource
    private GoodsSpecialContentTService goodsSpecialContentTService;

    @Resource
    private GoodsSpecialTService goodsSpecialTService;

    private Map<String,Object> responsedata=new HashMap<>();


    @Override
    public ResponseEntity save(@RequestBody GoodsSpecialContentT rq) {
        return null;
    }


    @RequestMapping(value="/find",method= RequestMethod.POST)
    @Override
    public ResponseEntity find(@RequestBody GoodsSpecialContentT rq) {
        if(StringUtils.isBlank(rq.getGoodsSpecialTId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            Criterion criterion= Restrictions.eq("goodsSpecialTId",rq.getGoodsSpecialTId());
            GoodsSpecialContentT bean=this.goodsSpecialContentTService.findOneByCriteria(GoodsSpecialContentT.class,criterion);
            if(bean!=null){
                GoodsSpecialT g=this.goodsSpecialTService.findByPK(GoodsSpecialT.class,rq.getGoodsSpecialTId());
                if(g!=null){
                    List<ProductSearchResultVo>list= GsonJson.parseJsonToData(bean.getContent(),ProductSearchResultVo.class);
                    responsedata.put(TagWords.BEAN,g);
                    responsedata.put(TagWords.BEANLIST,list);
                    responsedata.put(TagWords.SUCCESS, true);
                    responsedata.put(TagWords.MSG,TagWords.OPERATION_SUCCESS);
                }
            }
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity del(@RequestBody GoodsSpecialContentT rq) {
        return null;
    }


    @RequestMapping(value="/update",method= RequestMethod.POST)
    @Override
    public ResponseEntity update(@RequestBody GoodsSpecialContentT rq) {
        if(StringUtils.isBlank(rq.getGoodsSpecialTId())){
            responsedata.put(TagWords.SUCCESS,false);
            responsedata.put(TagWords.MSG, WarnWrods.ERROR_PARAMS);
        }else{
            Criterion criterion= Restrictions.and(Restrictions.eq(TagWords.STATUS, BaseEnums.DataUsingState.USING.getState())).add(Restrictions.eq("goodsSpecialTId",rq.getGoodsSpecialTId()));
            GoodsSpecialContentT bean=this.goodsSpecialContentTService.findOneByCriteria(GoodsSpecialContentT.class,criterion);
            if(bean!=null){
                bean.setContent(rq.getContent());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(bean.getVersiont()+1);
                this.goodsSpecialContentTService.updateGoodsSpecialContentTransactional(bean);
            }else{
                bean=new GoodsSpecialContentT();
                bean.setId(this.serial.Serialid(SerialKeys.GOODSSPECIALTCONTENT));
                bean.setContent(rq.getContent());
                bean.setCreatetime(DateTools.getSystemTimeBi());
                bean.setCreatorid(BaseTools.getAdminCreateId());
                bean.setGoodsSpecialTId(rq.getGoodsSpecialTId());
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(1);
                this.goodsSpecialContentTService.saveGoodsSpecialContentTransactional(bean);
            }
            responsedata.put(TagWords.SUCCESS, true);
            responsedata.put(TagWords.MSG, TagWords.OPERATION_SUCCESS);
        }
        return Response.success(responsedata);
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @Override
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype) {
        return null;
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {

    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {

    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {

    }

    @Override
    public void processPageList(List<GoodsSpecialContentT> list) {

    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
